package com.echo.week;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author echo
 * @Description
 * @Date 2023/10/16.
 * @Version 1.0
 */
public class DemoVolatile {
    public static void main(String[] args) throws InterruptedException {

        Demo demo = new Demo();
        for (int i = 0; i < 2; i++) {
            Thread t = new Thread(demo);
            t.start();
        }
        Thread.sleep(1000);

        System.out.println(demo.count);
    }

    static class Demo implements Runnable {
//        public volatile int count = 0;
//        public volatile AtomicInteger count = new AtomicInteger(0);

        public int count = 0;

        public ReentrantLock lock = new ReentrantLock();

        @Override
        public void run() {
            addCount();
        }

//        public synchronized void addCount() {
        public void addCount() {
            lock.lock();
            for (int i = 0; i < 10000; i++) {
//                count.incrementAndGet();
                count++;
            }
            lock.unlock();
        }
    }
}
